
#ifdef WIN32
#include <io.h>
#include <shlwapi.h>
#include <process.h>
#pragma comment(lib, "shlwapi")
#pragma comment(lib, "User32.lib")
#pragma warning(disable:4996)

#else
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include<pthread.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <semaphore.h>
#include <sys/syscall.h>
#endif

#include <string>
#include <string.h>
#include "SLT_filesystem.h"

static void SLT_fixPath(std::string& path)
{
    for (std::string::iterator iter = path.begin(); iter != path.end(); ++iter)
    {
        if (*iter == '\\') { *iter = '/'; }
    }
    if (path.at(path.length() - 1) != '/') { path.append("/"); }
}

bool SLT_isDirectory(const char * _path)
{
#ifdef WIN32
    return PathIsDirectoryA(_path) ? true : false;
#else
    DIR* pdir = opendir(_path);
    if (pdir == NULL)
    {
        return false;
    }
    else
    {
        closedir(pdir);
        pdir = NULL;
        return true;
    }
#endif
}


bool SLT_createRecursionDir(const char* _path)
{
    if (! _path) {
        return false;
    } 
    std::string sub;

    std::string path(_path);
    SLT_fixPath(path);

    std::string::size_type pos = path.find('/');
    while (pos != std::string::npos)
    {
        std::string cur = path.substr(0, pos - 0);
        if (cur.length() > 0 && !SLT_isDirectory(cur.c_str()))
        {
            bool ret = false;
#ifdef WIN32
            ret = CreateDirectoryA(cur.c_str(), NULL) ? true : false;
#else
            ret = (mkdir(cur.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0);
#endif
            if (!ret)
            {
                return false;
            }
        }
        pos = path.find('/', pos + 1);
    }

    return true;
}

uint32_t SLT_getProcessID()
{
#ifdef WIN32
    return GetCurrentProcessId();
#else
    return getpid();
#endif
}


void SLT_getProcessName(char *_name,int _namelen)
{
    std::string name = "process";
    char buf[500] = { 0 };
#ifdef WIN32
    if (GetModuleFileNameA(NULL, buf, 259) > 0)
    {
        name = buf;
    }
    std::string::size_type pos = name.rfind("\\");
    if (pos != std::string::npos)
    {
        name = name.substr(pos + 1, std::string::npos);
    }
    pos = name.rfind(".");
    if (pos != std::string::npos)
    {
        name = name.substr(0, pos - 0);
    }
    snprintf(_name, _namelen, name.c_str());
#elif defined(LOG4Z_HAVE_LIBPROC)
    proc_name(getpid(), buf, 260);
    name = buf;
    snprintf(_name, _namelen, name.c_str());
#else
    sprintf(buf, "/proc/%d/cmdline", (int)getpid());
    FILE *_file = fopen(buf, "rb");
    if (!_file)
    {
        snprintf(_name, _namelen,"%s", name.c_str());
        return;
    }
    memset(buf, 0, sizeof(buf));
    if (fgets(buf, sizeof(buf), _file) == NULL)
    {
        snprintf(_name, _namelen,"%s", name.c_str());
        return;
    }
    name = buf;
    fclose(_file);

    std::string::size_type pos = name.rfind("/");
    if (pos != std::string::npos)
    {
        name = name.substr(pos + 1, std::string::npos);
    }
    snprintf(_name, _namelen,"%s", name.c_str());
#endif    
}